import { getTopics } from '~/server/database/services/topicService'
import type { TopicDomain, Difficulty, ReviewStatus } from '~/server/database/models/Topic'

export default defineEventHandler(async (event) => {
  try {
    const query = getQuery(event)
    
    // 解析查询参数
    const params = {
      domain: query.domain as TopicDomain || undefined,
      difficulty: query.difficulty as Difficulty || undefined,
      reviewStatus: query.reviewStatus as ReviewStatus || undefined,
      submitterId: query.submitterId ? parseInt(query.submitterId as string) : undefined,
      page: query.page ? parseInt(query.page as string) : 1,
      limit: query.limit ? parseInt(query.limit as string) : 10,
      search: query.search as string || undefined
    }

    // 获取题目列表
    const result = await getTopics(params)

    return {
      success: true,
      data: result
    }
  } catch (error) {
    console.error('获取题目列表失败:', error)
    throw createError({
      statusCode: 500,
      statusMessage: '获取题目列表失败'
    })
  }
})
